<template>
  <div class="paperWeightReduction">

    <el-card class="paper-card">
      <div slot="header" class="clearfix">
        <div class="title">降AIGC率&nbsp;
          <el-tooltip effect="dark" placement="right">
            <div slot="content">
              1、输入需要降AIGC的文本，点击“降AIGC”按钮进行处理。
              <p>2、降AIGC结果将显示在右侧文本框中。</p>
              3、点击“复制结果”按钮，可以将降AIGC结果复制到剪贴板。
            </div>
            <i class="el-icon-question"></i>
          </el-tooltip>
        </div>
      </div>
      <el-row :gutter="10">
        <el-col :span="11">
          <el-input
              type="textarea"
              :rows="13"
              placeholder="请输入转化的内容，最多1000个字符......"
              v-model="inputText"
              maxlength="1000"
              show-word-limit
          >
          </el-input>
        </el-col>
        <el-col :span="2" class="btnCol">
          <div @click="handleReduceAIGC" :class="btnDisabled ? 'btn-disabled': 'btn'">
            <i class="el-icon-arrow-right"></i>&nbsp;降AIGC
          </div>
          <div @click="copyText" class="btn">
            <i class="el-icon-copy-document"></i>&nbsp;复制
          </div>
          <div @click="clearText" class="btn">
            <i class="el-icon-delete"></i>&nbsp;清除
          </div>
        </el-col>
        <el-col :span="11">
          <el-input
              type="textarea"
              :rows="13"
              placeholder="转化结果......"
              v-model="outputText"
              readonly
          >
          </el-input>
        </el-col>
      </el-row>
      <el-row class="paper-info" v-show="paperInfoShow">
        <el-col :span="24">
          <el-progress
              :percentage="progress" status="success" :stroke-width="7"
              :status="progress === 100 ? 'success' : undefined"
          ></el-progress>
        </el-col>
        <el-row class="paperAmountInfo">
          <el-col :span="24">本次消耗详情：</el-col>
          <el-col :span="4">字符数：{{amount.amountChar}}字符</el-col>
          <el-col :span="4">金额：{{amount.amountMoney}}元</el-col>
        </el-row>
      </el-row>
    </el-card>

    <el-row class="use-info">
      <el-col :span="24">
        <div class="title">注意：</div>
        <div class="content">
          <div>1、实际消耗字符数请以结果中的字符数为准。</div>
          <div>2、为保证最好的降AIGC效果，单次降AIGC率输入限制1000字符。降AIGC可能会导致前后字符数有差异，属于正常现象。
          </div>
          <div>3、如有大量文本需要降AIGC，请联系人工客服进行处理。由于人工介入，因此收费会有所不同。</div>
          <div>4、输入内容仅支持中文，其他语言正在陆续支持中。</div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {debounce} from 'lodash'

export default {
  data() {
    return {
      // 按钮是否禁用
      btnDisabled: false,

      inputText: '',
      outputText: '',

      // 进度条
      progress: 0,
      // 显示调用结构说明
      paperInfoShow:false,
      // 单次调用消耗
      amount: {
        amountChar: "*",
        amountMoney: "*",
      }
    };
  },
  created() {
    // 在组件创建时初始化防抖函数（确保单例）
    this.debouncedReduceAIGC = debounce(this.doReduceAIGC, 1000, {
      leading: true,  // 首次点击立即执行
      trailing: false // 忽略后续频繁点击
    })
  },

  methods: {
    handleReduceAIGC() {
      if (this.btnDisabled) return

      if(this.inputText.length===0) {
        this.$message.error("请输入内容！");
        return;
      }
      this.debouncedReduceAIGC()
    },
    async doReduceAIGC() {
      if(!this.$kunUtil.isLogin()){
        return;
      }
      this.$message.info('正在处理中，请稍后！');
      this.startSimulation();
      this.btnDisabled = true;
      this.paperInfoShow = true;
      await this.$api.main.reduceAIGC(this.inputText)
          .then((res) => {
            console.log(res.data)
            // 这里调用翻译API
            this.outputText = res.data.data.reduceAIGCText;
            this.amount= {
              amountMoney: res.data.data.amountMoney,
              amountChar:  res.data.data.amountChar
            }
          })
          .catch((err) => {
            console.log(err)
          })
      this.btnDisabled = false;
      this.progress = 100;
    },
    startSimulation() {
      this.progress = 0

      const interval = setInterval(() => {
        if (this.progress >= 100) {
          clearInterval(interval)
          return
        }

        // 动态随机增量算法
        const getRandomIncrement = () => {
          const remaining = 100 - this.progress

          // 根据当前进度动态调整随机范围
          if (this.progress < 30) {
            return Math.floor(Math.random() * 15 + 5) // 5-20
          } else if (this.progress < 70) {
            return Math.floor(Math.random() * 10 + 3) // 3-13
          } else {
            const maxStep = Math.min(5, remaining)
            return Math.floor(Math.random() * maxStep + 1) // 1-5
          }
        }

        this.progress = Math.min(
            this.progress + getRandomIncrement(),
            100
        )
      }, Math.random() * 300 + 200) // 随机间隔 200-500ms
    },

    // 复制文本到剪贴板
    copyText() {
      navigator.clipboard.writeText(this.outputText);
      this.$message.success('复制成功！');
    },
    // 清空文本
    clearText(){
      this.outputText = '';
      this.inputText = '';
      this.progress = 0;
      this.paperInfoShow = false;
      this.amount = {
        amountChar: "*",
        amountMoney: "*",
      }
      this.btnDisabled = false;
      this.$message.success('清除成功！');
    }
  }
};
</script>

<style scoped lang="less">

.paper-card {
  width: 100%;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border-radius: 10px;
  overflow: hidden;

  .clearfix {
    display: flex;

    .title {
      margin-right: 10px;
      font-size: 18px;
      font-weight: bold;
      color: #303133;
    }
  }

  .btnCol {
    min-height: 100px;
    padding-top: 80px;
    user-select: none;

    .btn {
      height: 40px;
      background-color: #409EFF;
      border-color: #409EFF;
      color: #ffffff;
      transition: all 0.3s ease;
      border-radius: 5px;
      margin-top: 10px;
      display: flex;
      justify-content: center;
      align-items: center;

      &:hover {
        background-color: #66b1ff;
        border-color: #66b1ff;
        cursor: pointer;

      }
    }

    .btn-disabled {
      height: 40px;
      background-color: #dcdfe6;
      border-color: #dcdfe6;
      cursor: not-allowed;
      color: #ffffff;
      transition: all 0.3s ease;
      border-radius: 5px;
      margin-top: 10px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }

  .paper-info{
    margin-top: 10px;
    .paperAmountInfo{
      color: #303133;
    }
  }
}

.use-info {
  margin-top: 20px;
  background-color: #ffffff;
  padding: 20px;
  border-radius: 10px;
  user-select: none;

  .title {
    font-size: 16px;
    font-weight: bold;
  }

  .content {
    color: #495057;
  }
}

.el-progress-bar__inner {
  transition: all 0.3s ease-in-out;
}

/deep/ .el-textarea__inner {
  resize: none;
  border-radius: 8px;
  border: 1px solid #dcdfe6;
  transition: border-color 0.3s ease;
}

/deep/ .el-textarea__inner:first-child:focus,
/deep/ .el-textarea__inner:last-child:hover {
  border-color: #409EFF;
  outline: 0;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}

</style>
